/*
 * Map.c
 *
 *  Created on: 11/11/2011
 *      Author: marcio
 */


#include "Map.h"

Map* map_newMap(int size, int (*fk)(void*, void*), int (*fi)(void*, void*)){
	int s = size < 0? -size : size;
	Map* m = malloc(sizeof(Map));
	m->compimage = fi;
	m->compkey = fk;
	m->images = malloc(sizeof(void*)*s);
	m->keys = malloc(sizeof(void*)*s);
	m->last = 0;
	m->size = s;
	return 0;
}

int map_add(Map* m, void* key, void* value){
	if(!map_isFull(m)){
		if(!map_seachForKey(key) && !map_seachForValue(value)){
			m->keys[m->last] = key;
			m->values[m->last++] = value;
		}
	}
	return 1;
}


int map_addAll(Map* m, void** key, void** value, int size){
	int i = 0;
	while(!map_isFull(m) && i < m->size){
		if( !map_add(m, key[i], value[i])) return 1;
		i++;
	}
	return 0;
}

void* map_getValue(Map* m, void* key){
	int i;
	for(i = 0; i < m->size; i++){
		if(m->compkey(key, m->keys[i])) return m->values[i];
	}
	return NULL;
}

void* map_getKey(Map* m, void* value){
	int i;
	for(i = 0; i < m->size; i++){
		if(m->compkey(key, m->keys[i])) return 0;
	}
	return 1;
}

int map_getAll(Map* m, void** key, void** value){
	int i;
	for(i = 0; i < m->size; i++){
		key[i] = m->keys[i];
		value[i] = m->values[i];
	}
	return 0;
}

int map_seachForKey(Map* m, void* key){
	int i;
	for(i = 0; i < m->size; i++){
		if(m->compkey(key, m->keys[i])) return 0;
	}
	return 1;
}

int map_searchForValue(Map* m, void* value){
	int i;
	for(i = 0; i < m->size; i++){
		if(m->compvalue(key, m->values[i])) return 0;
	}
	return 1;
}

int map_isEmpty(Map* m){
	return m->last == 0;
}

int map_isFull(Map* m){
	return m->last == m->size;
}
